<?php
/**
 *
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package
 * @subpackage
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * @property int $min_length
 * @property int $max_length
 */
class DjCharField extends DjField {


    /**
     * return the cleaned value
     * if value does not validate, raise a DjValidationError
     * @param mixed $input
     * @return mixed
     */
    public function clean($input) {
        // checking empty/required
        $this->_cleanRequired($input);
        if (!is_string($input)) {
            throw new DjValidationError("has to be a string");
        }
        // checking length
        if ($this->min_length and (strlen($input) < $this->min_length))
            throw new DjValidationError("Minimum length is ".$this->min_length);
        if ($this->max_length and (strlen($input) > $this->max_length))
            throw new DjValidationError("Maximum length is ".$this->max_length);
        return $input;
    }



    protected $max_length;
    protected function _max_length_check($x) {
        return is_integer($x);
    }
    // this updates widget maxlength attribute
    protected function _max_length_inputFilter($x) {
        $this->widget->setAttribute('maxlength', strval($x));
        return $x;
    }

    protected $min_length;
    protected function _min_length_check($x) {
        return is_integer($x);
    }

    /**
     * default widget is <input type="text" />
     */
    protected function _widget_autoValue() {
        return new DjTextInput();
    }
}